package top.hmtools.ckfinder.base;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 基础controller
 * @author Hybomyth
 *
 */
public abstract class BaseController{
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 获取线程安全的当前请求的HttpServletRequest对象实例
	 * @return
	 */
	protected HttpServletRequest getRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	/**
	 * 获取线程安全HttpServletResponse对象实例
	 * @return
	 */
	protected HttpServletResponse getResponse(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
	}
	
	/**
	 * 获取当前请求的session对象实例
	 * @return
	 */
	protected HttpSession getSession(){
		return this.getRequest().getSession();
	}
	
	/**
	 * 获取请求参数集合
	 * @return
	 */
	protected Map<String, String[]> getParams(){
		return this.getRequest().getParameterMap();
	}
	
	/**
	 * 获取本项目url域名+端口号（80端口时则忽略）+项目路径（如果有配置的话）
	 * @return
	 */
	protected String getURLPrefix(){
		HttpServletRequest request = this.getRequest();
		StringBuffer requestURL = request.getRequestURL();
		String requestURI = request.getRequestURI();
		int serverPort = request.getServerPort();
		String contextPath = request.getContextPath();
		
		StringBuffer result = new StringBuffer();
		if(serverPort==80){
			result.append(requestURL.toString().replaceAll(requestURI, "").replaceAll(":"+serverPort, ""));
		}else{
			result.append(requestURL.toString().replaceAll(requestURI, ""));
		}
		if(contextPath != null && contextPath.length()>0){
			result.append(contextPath);
		}
		return result.toString();
	}
	
	/**    
     * 统一异常处理 之 请求参数异常    
     * @return    
     */      
//    @ExceptionHandler({XqcpParamInException.class})       
//    public ModelAndView exception(XqcpParamInException e) {      
//    	ModelAndView mav = new ModelAndView();
//    	mav.setStatus(HttpStatus.BAD_REQUEST);
//    	mav.addObject(CommonsConfiguration.RTN_STATUS, HttpStatus.BAD_REQUEST.value());
//    	mav.addObject(CommonsConfiguration.RTN_MSG, e.getMessage());
//        return mav;       
//    }   

    /**    
     * 统一异常处理 
     * @return    
     */      
//    @ExceptionHandler({RuntimeException.class})       
//    public ModelAndView exception(RuntimeException e) {      
//    	ModelAndView mav = new ModelAndView();
//    	mav.setStatus(HttpStatus.BAD_REQUEST);
//    	mav.addObject(CommonsConfiguration.RTN_STATUS, HttpStatus.BAD_REQUEST.value());
//    	mav.addObject(CommonsConfiguration.RTN_MSG, e.getMessage());
//    	if(
//    			!(e instanceof XqcpParamInException) &&
//    			!(e instanceof XqcpUserUnloginedException) &&
//    			!(e instanceof XqcpDataException) &&
//    			!(e instanceof XqcpDataExistException) &&
//    			!(e instanceof XqcpServiceFlowException) &&
//    			!(e instanceof XqcpDeleteException)
//    			){
//    		this.logger.error(e.getMessage(),e);
//    	}
//        return mav;       
//    }  
    
}


